<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;

class Login extends Base
{
    public function index() {
    	
    	/*$salt = substr(uniqid(),-6);
    	dump($salt);
    	halt(CorePwd('123456',$salt));*/
    	
    	$this->view->engine->layout(false); 
        return $this->out();

    }
    
    public function signin(){
		
		$P = input('param.');
		$field = 'username';
		//判断类型
		$user = Db::name('user')->where($field,$P['account'])->find();
//		halt($user);
		if(!$user){
			return $this->error('无此用户');
		} elseif ( CorePwd($P['password'],$user['salt']) != $user['password']  ) {
			return $this->error('密码错误');
		}
		
		/*if(!$captcha->check($P['verify'])){
			return $this->_tip('验证码错误','verify');
		}*/
		
		$user['expire'] = $P['remember'] ? 3600*24*7 : 3600*24;
		//写入COOKIE\SESSION
//		$this->ip = request()->ip();
		$this->_login($user);
		
		//登录信息
//		Db::name('UserProfile')->where('user_id',$user['user_id'])->exp('lastlogin','NOW()')->update(['lastip'=>$this->ip]);
		if($P['refer']) {
			$url = $P['refer'];
		} else {
			$url = cookie('_refer') ?: '/admin';
		}
		
		return $this->success('登录成功',$url);
	}
	
	public function singout() {
	}
	
    //登录
	protected function _login($data, $log = 1) {
		
		cookie('user_id',StrEncrypt($data['user_id']),intval($data['expire']));
		cookie('user_check',StrEncrypt($data['password']),intval($data['expire']));
		session('user_id',$data['user_id']);
		$this->user_id = $data['user_id'];
		//操作日记
	}
    
}
